/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.scribeplugin;

import java.io.File;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractProtocolWriter implements Serializable {
	
	private static final long serialVersionUID = 4910632627857050143L;
	public static final String FILE_PATTERN = "file:/.+";
	
	/**
	 * Checks if given string is an file path
	 * @param str
	 * @return
	 */
	public boolean isFile(String str) {
		return getFile(str) != null;
	}
	
	/**
	 * filters all file strings from given string list
	 * @param lst list to filter
	 * @return filtered list
	 */
	public List<String> getFileList(List<String> lst) {
		if(lst == null)
			return null;
		
		List<String> filteredList = new LinkedList<String>();
		
		for(String str: lst) {
			if(isFile(str))
				filteredList.add(str);
		}
		
		return filteredList;
	}
	
	public File getFile(String fileString) {
		if(!fileString.matches(FILE_PATTERN))
			return null;
		
		File f = null;
		
		try {
			f = new File(new URI(fileString));
		} catch(URISyntaxException e) {
			
		}
		
		return f;
	}
}
